import store from '@/store/index.js';
const util = {
  creatTheArr(len, fill) {
    return new Array(len).fill(fill)
  },
  isTheType(v, Types) {
    const theType = util.typeOf(v);
    util.typeOf(Types) !== util.typeOf([]) && (Types = [Types])
    Types = Types.map(type => `[object ${type.name}]`)
    return Types.includes(theType)
  },
  typeOf(variable) {
    return Object.prototype.toString.call(variable);
  },
  isNull(value) {
    if (value === null) return true;
    if (value === undefined) return true;
    return value.toString().trim() === "";
  },
  serialize(obj) {
    let res = [];
    for (let key in obj) {
      res.push(`${key}=${obj[key]}`)
    }
    return res.join("&")
  },
  makeUrlCarryOptions(url, options) {
    url += url.indexOf('?') === -1 ? '?' : '&';
    url += util.serialize(options);
    return url;
  },
  addPrefixForObjKey(obj, preStr) {
    const res = {};
    const objKeys = Object.keys(obj);
    objKeys.forEach(key => res[preStr + key[0].toUpperCase() + key.slice(1)] = obj[key]);
    return res;
  },
  filterKeyByStandard(target, standard) {
    const standardKeys = Object.keys(standard);
    const targetKeys = Object.keys(target);
    targetKeys.forEach(key => {
      !standardKeys.includes(key) && delete target[key]
    })
    return target;
  },
  deleteEleFromArr(arr, ele) {
    return arr.filter(item => item === ele)
  },
  deserialization(str) {
    try {
      str = decodeURIComponent(decodeURIComponent(str)).split('&');
      console.warn("j解码内容",str)
      return str.reduce((sum, item) => {
        item = item.split('=');
        sum[item[0]] = item[1];
        return sum;
      }, {})
    } catch (e) {
      return {}
    }
  },
  getDateStr(date, ymd = false) {
    date = date || new Date();
    date = new Date(date);
    let Y = date.getFullYear() + "-";
    let M = (date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1) + "-";
    let D = (date.getDate() < 10 ? "0" + date.getDate() : date.getDate()) + " ";
    let h = (date.getHours() < 10 ? "0" + date.getHours() : date.getHours()) + ":";
    let m = (date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes()) + ":";
    let s = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
    let res = Y + M + D;
    !ymd && (res += h + m + s);
    return res;
  },
  getBirthdayByAge(age, ymd = true) {
    const birthday = new Date();
    birthday.setFullYear(birthday.getFullYear() - age);
    return `${birthday.getFullYear()}-01-01`;
  },
  /////////分割线///////////////

  checkRequire(formData, errTip) {
    for (let key in errTip) {
      if (!formData[key]) {
        uni.toast(errTip[key]);
        return false;
      }
    }
  },
  getPageOptions() {
    return util.getCurrentPage().options
  },
  getFullPagePath() {
    return util.getCurrentPage().$page.fullPath;
  },
  getCurrentPage() {
    return util.getArrayLast(getCurrentPages())
  },
  getArrayLast(array) {
    return array[array.length - 1]
  },
  getCurrentPageUrlWithArgs() {
    const pages = getCurrentPages()
    const currentPage = pages[pages.length - 1]
    const url = currentPage.route
    const options = currentPage.options
    let urlWithArgs = `/${url}?`
    for (let key in options) {
      const value = options[key]
      urlWithArgs += `${key}=${value}&`
    }
    urlWithArgs = urlWithArgs.substring(0, urlWithArgs.length - 1)
    return urlWithArgs
  },
  // 判断当前时间是否小于本地存储时间(24小时)
  /* fromTime:存储时间 ,toTime:当前时间 */
  getDiff(fromTime,toTime){
    let a1 = new Date(fromTime);
    let a2 = new Date(toTime);
    // 相差的秒数
    let total = (a2.getTime() - a1.getTime()) / 1000;
    // 计算是否超过24小时
    let endTime = parseInt(total/(60*60));
    if(endTime > 24){
      return false
    }else{
      return true;
    }
  },
  getDayBetween(time1,time2){
    let a1 = new Date(time1);
    let a2 = new Date(time2);
    var days=(endDate - startDate)/(1*24*60*60*1000);
    return days;
  },
  // 获取当前小程序环境的appid并存到vuex
  wxAppidToCompanyCode(){
    const accountInfo = wx.getAccountInfoSync();
    const {appId} = accountInfo.miniProgram;
    const companyCode = appId === 'wx7e72454a31cea08a' ? 'YJY' : 'CBH'
    store.commit('setCompanyCode', companyCode)
  },
  /*
    * @description: 获取对应日期的加几天返回的日期
    * @param {*} date 对应日期
    * @param {*} num 加几天数量
    * @return {*}
   */
  addDate(date,num) {
    let dates = new Date(date);
    dates.setDate(dates.getDate() + Number(num));
    let year = dates.getFullYear();
    let month = dates.getMonth() + 1;
    let day = dates.getDate();
    if (month < 10) {
      month = month + '0'
    }
    if (day < 10) {
      day = day + '0'
    }
    let time = year + '-' + month + '-' + day
    return time;
  }
}
export default util;
